@extends('backend/layouts/default')
@section('title')
Sửa người dùng ::
@parent
@stop
@section('content')
<h3>
    <span class="glyphicon glyphicon-user"></span> Cập nhật thông tin người dùng
</h3>
<form class="form-horizontal" role="form" method="post" action="" autocomplete="off">
    {{Form::token()}}
    <div class="box box-solid">
        <div class="box-header">

        </div>
        <div class="box-body">
            <div class="row">
                <div class="col-sm-4">
                    <h4>Thông tin cơ bản</h4>

                    <div>
                        <div class="form-group {{ $errors->has('username') ? 'error' : '' }}">
                            <label class="control-label col-md-4" for="username">Tên hiển thị:</label>

                            <div class="col-md-8">
                                <input class="form-control" type="text" disabled="disabled" name="data[User][username]"
                                       id="data[User][username]" value="{{$data['User']->username}}"/>
                                {{ $errors->first('username', '<span class="help-inline">:message</span>') }}
                            </div>
                        </div>
                        <div class="form-group {{ $errors->has('email') ? 'error' : '' }}">
                            <label class="control-label col-md-4" for="email">Email:</label>

                            <div class="col-sm-8">
                                <input class="form-control" type="text" disabled="disabled"
                                       value="{{$data['User']->email}}" name="data[User][email]"
                                       id="data[User][email]"/>
                                {{ $errors->first('email', '<span class="help-inline">:message</span>') }}
                            </div>
                        </div>

                        <!--                        password-->

                        <div class="form-group {{ $errors->has('password') ? 'error' : '' }}">
                            <label class="control-label col-md-4" for="username">Mật khẩu:</label>

                            <div class="col-sm-8">
                                <input class="form-control" type="password" value="" name="data[User][password]"
                                       id="data[User][password]"/>
                                {{ $errors->first('password', '<span class="help-inline">:message</span>') }}
                            </div>
                        </div>

                        <!--                        confirm password-->

                        <div class="form-group {{ $errors->has('password_confirm') ? 'error' : '' }}">
                            <label class="control-label col-md-4" for="username">xác nhận mật khẩu:</label>

                            <div class="col-sm-8">
                                <input class="form-control" type="password" value="" name="data[User][password_confirm]"
                                       id="data[User][password_confirm]"/>
                                {{ $errors->first('password_confirm', '<span class="help-inline">:message</span>') }}
                            </div>
                        </div>
                        <!--                        activation status-->
                        <div class="form-group {{ $errors->has('activated') ? 'error' : '' }}">
                            <label class="control-label col-md-4" for="activated">Kích hoạt</label>

                            <div class="col-md-8">
                                <select class="form-control" name="data[User][activated]" {{ ($data['User']->id ===
                                Sentry::getId() ? 'disabled="disabled"' : '') }} name="activated" id="activated">
                                <option value="1"
                                {{ ($data['User']->isActivated() ? ' selected="selected"' :
                                '')}}>@lang('general.yes')</option>
                                <option value="0"
                                {{ ( ! $data['User']->isActivated() ? ' selected="selected"' :
                                '')}}>@lang('general.no')</option>
                                </select>
                                {{ $errors->first('activated', '<span class="help-inline">:message</span>') }}
                            </div>
                        </div>
                        <!--                        group-->
                        @if(Permission::has_access('user','permission',$data['User']->userid))
                        <div class="form-group {{ $errors->has('groups') ? 'error' : '' }}">
                            <label class="control-label col-md-4" for="groups">Nhóm</label>

                            <div class="col-md-8">
                                <select class="form-control" name="data[Groups][]" id="data[Groups][]" multiple>
                                    @foreach($data['Groups'] as $group)
                                    <option value="{{$group->id}}"
                                    {{(array_key_exists($group->id,$data['userGroups']))?' selected="selected"' :
                                    ''}}>{{$group->name}}</option>
                                    @endforeach
                                </select>
                                <span class="help-block">
                                    Chọn nhóm để gán quyền cho người dùng, mỗi người dùng chỉ có quyền hạn trên các nhóm mà họ được gán quyền.
                                </span>
                            </div>
                        </div>
                        @else
                        <select class="form-control hidden" name="groups[]" id="groups[]" multiple>
                            @foreach ($data['Groups'] as $group)
                            <option value="{{ $group->id }}"
                            {{ (array_key_exists($group->id, $data['userGroups']) ? ' selected="selected"' : '') }}>{{
                            $group->name }}</option>
                            @endforeach
                        </select>
                        @endif
                    </div>
                </div>
                <div class="col-sm-4">
                    @if(Permission::has_access('user','permission',$data['User']->id))
                    <h4>Phân quyền chuyên mục</h4>

                    <div>
                        <table class="table table-bordered">
                            <tr>
                                <th>Tên</th>
                                <th style="width: 40px"><span class="label label-success">Cho phép</span></th>
                                <th style="width: 40px"><span class="label label-danger">Từ chối</span></th>
                                <th style="width: 40px"><span class="label label-warning">Kế thừa</span></th>
                            </tr>
                            @foreach($data['catPermissions'] as $key => $cps)
                            @foreach($cps as $cp)
                            <tr>
                                <td><strong>{{$cp['label']}}</strong></td>
                                <td>
                                    <input type="radio" value="1" id="{{$cp['permission']}}_allow"
                                           name="data[User][permissions][{{$cp['permission']}}]" {{
                                    (array_get($data['User']['permissions'], $cp['permission']) === 1 ? '
                                    checked="checked"' : '') }} />
                                </td>
                                <td>
                                    <input type="radio" value="-1" id="{{$cp['permission']}}_deny"
                                           name="data[User][permissions][{{$cp['permission']}}]" {{
                                    (array_get($data['User']['permissions'], $cp['permission']) === -1 ? '
                                    checked="checked"' : '') }} />
                                </td>
                                <td>
                                    <input type="radio" value="0" id="{{$cp['permission']}}_inherit"
                                           name="data[User][permissions][{{$cp['permission']}}]" {{
                                    (array_get($data['User']['permissions'], $cp['permission']) === 0 ? '
                                    checked="checked"' : '') }} />
                                </td>
                            </tr>
                            @endforeach
                            @endforeach
                        </table>
                    </div>
                    @endif
                </div>
                <div class="col-sm-4">
                    @if(Permission::has_access('user','permission',$data['User']->id))
                    <h4>Quyền hạn</h4>

                    <div>
                        @foreach($data['Permissions'] as $area => $permissions)
                        <table class="table table-bordered">
                            <tr>
                                <th>{{ $area }}</th>
                                <th style="width: 40px"><span class="label label-success">Cho phép</span></th>
                                <th style="width: 40px"><span class="label label-danger">Từ chối</span></th>
                                <th style="width: 40px"><span class="label label-warning">Kế thừa</span></th>
                            </tr>
                            @foreach ($permissions as $permission)
                            <tr>
                                <td>{{ $permission['label'] }}</td>
                                <td>
                                    <input type="radio" value="1" id="{{ $permission['permission'] }}_allow"
                                           name="data[Permissions][{{ $permission['permission'] }}]"{{
                                    (array_get($data['User']['permissions'], $permission['permission']) === 1 ? '
                                    checked="checked"' : '') }}>
                                </td>
                                <td>
                                    <input type="radio" value="-1" id="{{ $permission['permission'] }}_deny"
                                           name="data[Permissions][{{ $permission['permission'] }}]"{{
                                    (array_get($data['User']['permissions'], $permission['permission']) === -1 ? '
                                    checked="checked"' : '') }}>
                                </td>
                                <td>
                                    @if ($permission['can_inherit'])<input type="radio" value="0"
                                                                           id="{{ $permission['permission'] }}_inherit"
                                                                           name="data[Permissions][{{ $permission['permission'] }}]"{{
                                    ( ! array_get($data['User']['permissions'], $permission['permission']) ? '
                                    checked="checked"' : '') }}>@endif
                                </td>
                            </tr>
                            @endforeach
                        </table>
                        @endforeach
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
    <div class="control-group" align="right">
        <div class="controls">
            <a class="btn btn-link" href="{{ route('users') }}">Hủy</a>
            <button type="submit" class="btn btn-success">Cập nhật</button>
        </div>
    </div>
</form>
@stop